package cn.player.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ListView;
import cn.player.adapter.ChannelInfoAdapter.ViewHolder;

public class ListViewCompat extends ListView {

	private SlideView mFocusedItemView;

	public ListViewCompat(Context context) {
		super(context);
	}

	public ListViewCompat(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public ListViewCompat(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public void shrinkListItem(int position) {
		View childAt = getChildAt(position + getHeaderViewsCount() - getFirstVisiblePosition());
		ViewHolder viewHolder = null;
		if (childAt != null) {
			viewHolder = (ViewHolder) childAt.getTag();
		}
		if (viewHolder != null) {
			viewHolder.slideView.shrink();
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN: {
			int x = (int) event.getX();
			int y = (int) event.getY();
			int position = pointToPosition(x, y);
			if (position != INVALID_POSITION) {
				View childAt = getChildAt(position + getHeaderViewsCount() - getFirstVisiblePosition());
				ViewHolder viewHolder = null;
				if (childAt != null) {
					viewHolder = (ViewHolder) childAt.getTag();
				}
				if (viewHolder != null) {
					mFocusedItemView = viewHolder.slideView;
				}
			}
			if (mFocusedItemView != null) {
				mFocusedItemView.setPosition(position);
			}
		}
		default:
			break;
		}

		if (mFocusedItemView != null) {
			mFocusedItemView.onRequireTouchEvent(event);
		}

		return super.onTouchEvent(event);
	}

}
